﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectSheduleAnalyst
{
    public partial class LimitationForm : Form
    {
        private DB_Access dba = new DB_Access();

        public LimitationForm()
        {
            InitializeComponent();
            MainForm main = this.Owner as MainForm;
            DataSet ds5 = dba.FillGrid("SelectAllClassRooms", "ClassRooms");
            dgvClassrooms.DataSource = ds5.Tables["ClassRooms"].DefaultView;
            dgvClassrooms.Columns[0].Visible = false;

            dgvClassrooms.Columns[1].HeaderText = "Номер";
            dgvClassrooms.Columns[2].HeaderText = "Тип";

        }



        private void button1_Click_1(object sender, EventArgs e)
        {
            MainForm main = this.Owner as MainForm;
            if (main != null)
            {
                main.classroom = dgvClassrooms.CurrentCell.Value.ToString();
                this.Close();
            }
        }

        private void AddClassroomButton_Click(object sender, EventArgs e)
        {
            AddChangeClassroomForm AddClass = new AddChangeClassroomForm();
            AddClass.Text = "Добавление новой аудитории";
            AddClass.AddChangeClassroomBtn.Text = "Добавить";

            if (AddClass.ShowDialog() == DialogResult.OK)
            {
                dba.MySQLquery(@"INSERT INTO CLASSROOMS (Number, CTYPE) VALUES
                    ('" + AddClass.ClassNumberTextBox.Text + "','"
                      + AddClass.idlist[AddClass.ClassTypeComboBox.SelectedIndex] + "')");

                DataSet ds5 = dba.FillGrid("SelectAllClassRooms", "ClassRooms");
                dgvClassrooms.DataSource = ds5.Tables["ClassRooms"].DefaultView;
            }
        }

        private void ChangeClassroomButton_Click(object sender, EventArgs e)
        {
            AddChangeClassroomForm ChngClass = new AddChangeClassroomForm();
            ChngClass.Text = "Изменение данных об аудитории";
            ChngClass.AddChangeClassroomBtn.Text = "Изменить";

            ChngClass.ClassNumberTextBox.Text = dgvClassrooms.CurrentRow.Cells[1].Value.ToString();

            //ChngClass.ClassTypeComboBox.SelectedIndex = 
            //есть помарочка с индексами когда во время работы добавляем новые типы аудиторий

            if (ChngClass.ShowDialog() == DialogResult.OK)
            {
                dba.MySQLquery(@"UPDATE CLASSROOMS
                SET Number = '" + ChngClass.ClassNumberTextBox.Text + "', CTYPE = '"
                                 + ChngClass.idlist[ChngClass.ClassTypeComboBox.SelectedIndex] + "' WHERE ID ='"
                                 + dgvClassrooms.CurrentRow.Cells[0].Value.ToString() + "' ");

                DataSet ds5 = dba.FillGrid("SelectAllClassRooms", "ClassRooms");
                dgvClassrooms.DataSource = ds5.Tables["ClassRooms"].DefaultView;
            }
        }

        private void DeleteClassroomButton_Click(object sender, EventArgs e)
        {
            if (
                MessageBox.Show("Удалить выбранную аудиторию?", "Предупреждение!",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                dba.MySQLquery("DELETE FROM CLASSROOMS WHERE ID='"
                    + dgvClassrooms.CurrentRow.Cells[0].Value.ToString() + "'");
            }

            DataSet ds5 = dba.FillGrid("SelectAllClassRooms", "ClassRooms");
            dgvClassrooms.DataSource = ds5.Tables["ClassRooms"].DefaultView;
        }









    }
}
